package com.zjgsu.nftplatform.controller;

import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.PurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2022/4/22 17:25
 * @description
 */
@Api(tags = "purchase合约")
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    private final PurchaseService purchaseService;

    public PurchaseController(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }
    
    @ApiOperation("查找出版权")
    @GetMapping("/publication/search")
    public Result searchPublication(@ApiParam("cid") @RequestParam String cid) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.searchPublication(cid));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看出版权")
    @GetMapping("/publication/{pid}")
    public Result getPublicationById(@ApiParam("pid") @PathVariable String pid) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.getPublicationById(pid));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("查看收益比例")
    @GetMapping("/stake/{pid}/{uaddr}")
    public Result getStakeById(@ApiParam("pid") @PathVariable String pid,
                               @ApiParam("uaddr") @PathVariable String uaddr) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.getStakeById(pid, uaddr));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("查看作者认定价格")
    @GetMapping("/price/{pid}/{uaddr}")
    public Result getPriceById(@ApiParam("pid") @PathVariable String pid,
                               @ApiParam("uaddr") @PathVariable String uaddr) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.getPriceById(pid, uaddr));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
    
    @ApiOperation("是否是卖家")
    @GetMapping("/isSeller")
    public Result isSeller(@ApiParam("cid") @RequestParam String cid,
                           @ApiParam("用户地址") @RequestParam String uaddr) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.isSeller(cid, uaddr));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("创建出版权协议")
    @PostMapping("/publication/create")
    public Result createPublication(@ApiParam("cid") @RequestParam String cid,
                                    @ApiParam("出版权期限（天）") @RequestParam String dayTime,
                                    @ApiParam("用户地址") @RequestParam String address,
                                    @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.createPublication(cid, dayTime, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("设置分红比例")
    @PostMapping("/set/stake")
    public Result setStake(@ApiParam("pid") @RequestParam String pid,
                           @ApiParam("stake") @RequestParam String stake,
                           @ApiParam("用户地址") @RequestParam String address,
                           @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.setStake(pid, stake, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("卖家设置价格")
    @PostMapping("/set/price")
    public Result setPrice(@ApiParam("pid") @RequestParam String pid,
                           @ApiParam("price") @RequestParam String price,
                           @ApiParam("用户地址") @RequestParam String address,
                           @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.setPrice(pid, price, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("同意出版权协议")
    @PostMapping("/publication/agree")
    public Result agreePublication(@ApiParam("id") @RequestParam String id,
                                   @ApiParam("price") @RequestParam String price,
                                   @ApiParam("用户地址") @RequestParam String address,
                                   @ApiParam("密码") @RequestParam String password) {
        try {
            return new Result(ResultCode.SUCCESS, purchaseService.agreePublication(id, price, address, password));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
